﻿//-----------------------------------------------------------------------
// <copyright file="GenericHandlerCaller.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Base.Message
{
    using System;
    using TileTool.Message;

    /// <summary>
    /// Generic message handler caller class.
    /// </summary>
    /// <typeparam name="T">Type of message we are expecting.</typeparam>
    internal class GenericHandlerCaller<T> : IHandlerCaller where T : MessageBase
    {
        #region Fields - Private

        /// <summary>
        /// Handler to call when a call is requested.
        /// </summary>
        private MessageHandler<T> handler;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor which takes the handler to call.
        /// </summary>
        /// <param name="handler">Handler to call.</param>
        public GenericHandlerCaller(MessageHandler<T> handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler", "Handler must be non-null.");
            }

            this.handler = handler;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Call the wrapped message handler with the passed message.
        /// </summary>
        /// <param name="msg">Message to handle.</param>
        /// <returns>Returns a message handler result which informs the messaging system
        /// if the handler handled the message or if it did not.</returns>
        public MessageHandlerResult Call(MessageBase msg)
        {
            T typedMsg = msg as T;
            if (typedMsg == null)
            {
                throw new ArgumentException("Message is not of proper type: " + typeof(T).Name, "msg");
            }

            return this.handler(typedMsg);
        }

        /// <summary>
        /// Equals overload.
        /// </summary>
        /// <param name="obj">Object to test for equality.</param>
        /// <returns>Returns true if this object is equal to the passed object.</returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (GetType() != obj.GetType())
            {
                return false;
            }

            GenericHandlerCaller<T> rhs = (GenericHandlerCaller<T>)obj;

            return this.handler == rhs.handler;
        }

        /// <summary>
        /// Compute a hash code for this object.
        /// </summary>
        /// <returns>Returns the hash code for the object.</returns>
        public override int GetHashCode()
        {
            return this.handler.GetHashCode();
        }

        #endregion
    }
}
